package com.cnebula.uas.sso.util;

import java.io.StringWriter;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;

public class JaxbUtil {

	// java对象转xml
	public static String beanToXml(Object obj) throws JAXBException {
		StringWriter writer = null;
		JAXBContext jaxbContext = JAXBContext.newInstance(obj.getClass());
		Marshaller marshaller = jaxbContext.createMarshaller();
		// Marshaller.JAXB_FRAGMENT:是否省略xml头信息,true省略，false不省略
		marshaller.setProperty(Marshaller.JAXB_FRAGMENT, true);
		// Marshaller.JAXB_FORMATTED_OUTPUT:决定是否在转换成xml时同时进行格式化（即按标签自动换行，否则即是一行的xml）
		marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
		// Marshaller.JAXB_ENCODING:xml的编码方式
		marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
		writer = new StringWriter();
		marshaller.marshal(obj, writer);
		return "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + writer.toString();
	}

}
